var couponTable = null;

var couponCtrl = avalon.define({
	$id : "coupon",
	param : {
		regionCode : "",
		storeCode : "",
		activityName : "",
		startTime : "",
		endTime : "",
		search : function() {
			couponTable.search({
				regionCode : couponCtrl.param.regionCode,
				storeCode : couponCtrl.param.storeCode,
				activityName : couponCtrl.param.activityName,
				startTime : couponCtrl.param.startTime,
				endTime : couponCtrl.param.endTime
			});
		}
	},
	checkword : function() {
		// 导出ajax
		var regionCode = couponCtrl.param.regionCode;
		var storeCode = couponCtrl.param.storeCode;
		var activityName = couponCtrl.param.activityName;
		var startTime = couponCtrl.param.startTime;
		var endTime = couponCtrl.param.endTime;
		$.ajax({
			type : "post",
			url : DI.checkSendCouponExport,
			data : {
				regionCode : regionCode,
				storeCode : storeCode,
				activityName : activityName,
				startTime : startTime,
				endTime : endTime
			},
			dataType : "json",
			error : function() {
				alert("系统繁忙,请稍后重试");
			},
			success : function(responseData) {
				if (responseData != null && responseData.retFlag == "1") {
					if (window.confirm("确定导出" + responseData.data + "条记录？")) {
						window.location.href = DI.exportSendCouponStat
								+ "?startTime=" + startTime + "&endTime="
								+ endTime + "&regionCode=" + regionCode
								+ "&storeCode=" + storeCode + "&activityName="
								+ encodeURIComponent(activityName);
					} else {
						return;
					}
				} else if (responseData.retFlag == "0") {
					PLATFORM.headTip("导出失败:" + responseData.errorMessage);
				} else {
					PLATFORM.headTip("导出失败");
				}
			}
		});
	}
});
avalon.scan();
$(function() {
	PLATFORM.datepickerRangeLimit("#timepickerStart", "#timepickerEnd", 90);
	couponCtrl.param.startTime = PLATFORM.getDayLimit(30);
	couponCtrl.param.endTime = PLATFORM.getDay();
	var magicRegion = $('#magicRegion').magicSuggest({
		placeholder : "选择大区",
		required : true,
		selectionStacked : true,
		expandOnFocus : true
	});
	$(magicRegion).on("selectionchange", function(e, self, records) {
		if (records.length > 0) {
			couponCtrl.param.region = records[0].name;
			couponCtrl.param.regionCode = records[0].id;
			$.ajax({
				type : "post",
				url : DI.queryStroeByRegionCode,
				data : {
					regionCode : records[0].id
				},
				dataType : "json",
				success : function(shops) {
					couponCtrl.allShop = shops.storeInfos;
					var allShopData = [];
					for ( var j = 0; j < couponCtrl.allShop.length; j++) {
						allShopData.push({
							id : couponCtrl.allShop[j].storeCode,
							name : couponCtrl.allShop[j].storeName
						});
					}
					magicShop.clear();
					magicShop.collapse();
					magicShop.setData(allShopData);
					magicShop.enable();
				},
				error : function() {

				}
			});
		} else {
			couponCtrl.param.regionCode = "";
			couponCtrl.param.storeCode = "";
			magicShop.clear();
			magicShop.collapse();
			magicShop.setData([]);
			magicShop.disable();
		}
	});
	var magicShop = $('#magicShop').magicSuggest({
		placeholder : "选择门店",
		required : true,
		selectionStacked : true,
		disabled : true,
		expandOnFocus : true
	});
	$(magicShop).on("selectionchange", function(e, self, records) {
		if (records.length > 0) {
			couponCtrl.param.shop = records[0].name;
			couponCtrl.param.storeCode = records[0].id;
		} else {
			couponCtrl.param.shop = "";
			couponCtrl.param.storeCode = "";
		}
	});
	$.ajax({
		type : "post",
		url : DI.queryAllRegion,
		dataType : "json",
		success : function(data) {
			couponCtrl.allRegion = data.regions;
			var allRegionData = [];
			for ( var i = 0; i < couponCtrl.allRegion.length; i++) {
				allRegionData.push({
					id : couponCtrl.allRegion[i].regionCode,
					name : couponCtrl.allRegion[i].regionName
				});
			}
			magicRegion.setData(allRegionData);
			magicRegion.setValue([allRegionData[0].id]);
			var opts_coupon = {
					targetId : "couponTb",
					templateId : "couponTbTemplate",
					settings : {
						url : DI.sendCouponStat,
						ajaxType : "json",
						ajaxDataHandler : function(data) {
							var temp = {};

							if (data.retFlag == "1" && data.data) {
								temp.total = data.data.totalSize;
								temp.rows = data.data.records;
							} else {
								if (data.errorMessage && data.errorMessage != "") {
									PLATFORM.headTip(data.errorMessage);
								}
								temp.rows = [];
							}

							return temp;
						},
						paginationIndexName : {
							"peerpageRows" : 'pageSize',
							"currentPage" : 'pageNo'
						},
						searchParam: {
							regionCode : couponCtrl.param.regionCode,
							storeCode : couponCtrl.param.storeCode,
							activityName : couponCtrl.param.activityName,
							startTime : couponCtrl.param.startTime,
							endTime : couponCtrl.param.endTime
						}
					},
				};
			couponTable = SwiftUI.swiftTable.getInstance(opts_coupon);
		},
		error : function(err) {
			console.log(err);
		}
	});
});
